/*
 * 软件著作权：北京康兰尚德数据科技有限公司
 * 项目名称：康兰数据购物中心分析平台
 *
 * NAME : CollectDaoImpl.java
 *
 * PURPOSE : 用户收藏数据库访问
 *
 * AUTHOR : EEDATA/ywb
 *
 *
 * 创建日期: 2013-11-27
 * HISTORY : 2013-11-28
 * 变更日期 
 */
package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import pojo.EcpGwcShopping;
import pojo.EcpPpBrand;
import pojo.EcpScUsercollect;
import util.HibernateUtils;

/**
 * 收藏数据访问层
 * 
 * @author ywb
 */

public class CollectDaoImpl {

    /**
     * 根据用户id和标识(1为品牌2为购物中心)获取品牌id或购物中心id
     */
    @SuppressWarnings("unchecked")
    public List<String> selectBrandOrShopId(Integer userid, Integer sort) {// 十一、getFavoritedShoppingCenterList接口
                                                                           // 十二、getFavoritedBrandInfoList接口

        Session session = null;
        Transaction ts = null;
        List<String> list = null;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            Query query =
                session
                    .createQuery("select fid from EcpScUsercollect where userid=? and sort=? and tag=1");
            query.setParameter(0, userid);
            query.setParameter(1, sort);
            list = query.list();

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return list;

    }


    /**
     * 根据品牌id获取品牌信息
     */
    public EcpPpBrand selectBrand(String brandid) { // 十二、getFavoritedBrandInfoList接口
        Session session = null;
        Transaction ts = null;
        EcpPpBrand brand = null;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            brand = (EcpPpBrand) session.get(EcpPpBrand.class, brandid);

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return brand;

    }


    /**
     * 根据购物中心id获取购物中心信息
     */
    public EcpGwcShopping selectShop(String shopid) {// 十一、getFavoritedShoppingCenterList接口
        Session session = null;
        Transaction ts = null;
        EcpGwcShopping shop = null;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            shop = (EcpGwcShopping) session.get(EcpGwcShopping.class, shopid);

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return shop;

    }


    /**
     * 收藏品牌或购物中心
     */
    public boolean insertCollect(EcpScUsercollect collect) {// 二十四、favoriteShoppingCenter接口
                                                            // 二十五、favoriteBrand接口
        Session session = null;
        Transaction ts = null;
        boolean row = true;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            session.persist(collect);

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            row = false;
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return row;

    }


    /**
     * 判断品牌或购物中心是否已经收藏
     */
    @SuppressWarnings("unchecked")
    public boolean checkCollect(EcpScUsercollect collect) {// 二十六、isFavoriteBrand接口
                                                           // 二十七、isFavoriteShoppingCenter接口
        Session session = null;
        Transaction ts = null;
        boolean row = false;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            Query query =
                session
                    .createQuery("select tag from EcpScUsercollect where userid=? and fid=? and sort=?");
            query.setParameter(0, collect.getUserid());
            query.setParameter(1, collect.getFid());
            query.setParameter(2, collect.getSort());

            List<Boolean> list = query.list();

            if (list.size() > 0)
                row = list.get(0);

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return row;

    }


    /**
     * 取消收藏品牌或购物中心
     */
    public boolean updateCollect(EcpScUsercollect collect) {// 二十八、cancelFavoriteBrand接口
                                                            // 二十九、cancelFavoriteShoppingcenter接口
        Session session = null;
        Transaction ts = null;
        boolean row = true;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            session.update(collect);

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            row = false;
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return row;

    }


    /**
     * 根据用户id和品牌或购物中心id获取收藏id(用于取消收藏)
     */
    @SuppressWarnings("unchecked")
    public EcpScUsercollect selectCollect(EcpScUsercollect collect) {// 二十八、cancelFavoriteBrand接口
                                                                     // 二十九、cancelFavoriteShoppingcenter接口
                                                                     // 二十四、favoriteShoppingCenter接口
                                                                     // 二十五、favoriteBrand接口
        Session session = null;
        Transaction ts = null;
        EcpScUsercollect usercollect = null;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            Query query =
                session.createQuery("from EcpScUsercollect where userid=? and fid=? and sort=?");
            query.setParameter(0, collect.getUserid());
            query.setParameter(1, collect.getFid());
            query.setParameter(2, collect.getSort());

            List<EcpScUsercollect> list = query.list();
            if (list.size() > 0)
                usercollect = list.get(0);

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return usercollect;

    }

}
